import { MessageContext } from 'vk-io';
import { Command } from '../core/commander';
import got from 'got';
import dedent from 'dedent';

interface CodeExecutionResult {
  response: {
    stdout?: string,
    stderr?: string,
    exec_time: number,
  }
}

export const anarchyCommand = new Command<MessageContext>({
  triggers: {
    regex: /код (?<language>.*) (?<code>(?:.|\n)*)/i,
  },

  handler: async (context, { language, code }) => {
    const { response }: CodeExecutionResult = await got.get(`${process.env.API_URL}/text/executeCode`, {
      searchParams: { language, code }
    }).json();

    await context.send(dedent`
      Stdout: ${response.stdout}
      Stderr: ${response.stderr}

      Выполнил за ${response.exec_time}
    `);
  },
});
